package com.rxyl.love.petdoctor.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.entity.GoodDoctor;
import com.rxyl.love.petdoctor.entity.HospitalDoctor;

import java.util.List;

/**
 * Created by Administrator on 2016/1/12 0012.
 */
public class MyGridViewAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> goodDoctorList;

    public MyGridViewAdapter(Context context, List<T> goodDoctorList) {
        this.context = context;
        this.goodDoctorList = goodDoctorList;
    }

    @Override
    public int getCount() {
        return goodDoctorList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodDoctorList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T doctor = goodDoctorList.get(position);
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_layout_gooddoctor, null);
            vh = new ViewHolder();
            vh.tv1 = (TextView) convertView.findViewById(R.id.tv_doctor_name);
            vh.tv2 = (TextView) convertView.findViewById(R.id.tv_doctor_info);
            vh.iv = (SimpleDraweeView) convertView.findViewById(R.id.iv_doctor_head);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (doctor instanceof GoodDoctor.DataEntity) {
            GoodDoctor.DataEntity entity = (GoodDoctor.DataEntity) doctor;
            vh.tv1.setText(entity.getName());
            vh.tv2.setText(entity.getJob());
            String url = entity.getAvatar();
        /*MyApplication.getApp().getImageLoader()
                .displayImage(url,vh.iv,MyApplication.getApp().getImageOptions());*/
            vh.iv.setImageURI(Uri.parse(url));
        } else if (doctor instanceof HospitalDoctor.DataEntity) {
            HospitalDoctor.DataEntity entity = (HospitalDoctor.DataEntity) doctor;
            vh.tv1.setText(entity.getName());
            vh.tv2.setText(entity.getJob());
            String url = entity.getAvatar();
        /*MyApplication.getApp().getImageLoader()
                .displayImage(url,vh.iv,MyApplication.getApp().getImageOptions());*/
            vh.iv.setImageURI(Uri.parse(url));
        }

        return convertView;
    }

    class ViewHolder {
        SimpleDraweeView iv;
        TextView tv1, tv2;
    }
}
